package cc.itez.wx.linkman.mould;

import cn.hutool.core.util.StrUtil;
import lombok.Data;
import lombok.experimental.Accessors;

import java.util.function.Function;

@Data
@Accessors(chain = true)
public class Command {
    /**
     * 命令
     */
    private final String cmd;
    /**
     * 命令名
     */
    private final String name;
    /**
     * 命令备注
     * 用于生成help信息
     */
    private final String desc;
    /**
     * 命令步骤
     */
    private final Function<WxMessage, String>[] functions;
    /**
     * 命令步骤长度
     */
    private final int size;

    @SafeVarargs
    public Command(String cmd, String name, String desc, Function<WxMessage, String>... functions) {
        this.cmd = cmd;
        this.name = name;
        this.desc = desc;
        this.functions = functions;
        this.size = functions.length;
    }

    public Function<WxMessage, String> func(int step) {
        if (this.getSize() > step) {
            return this.functions[step];
        }
        return null;
    }

    public String toHelpString() {
        return StrUtil.format("{}\t\t{}\n{}", this.cmd, this.name, this.desc);
    }
}
